package cn.bonoon.kernel.menus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bonoon.kernel.util.StringHelper;

/**
 * 关于平台、子系统、频道或菜单的一些描述说明，应该也包括这个功能的帮助文档之类的；
 * 
 * 接下来的目的：每个功能可以定义自己的帮助文档
 * 
 * 问题：如果方便的定义和修改帮助文档，是否允许有权限的用户，在特定的功能上添加帮助文档，如：上传操作视频等
 * 
 * @author jackson
 *
 */
class DescribeDefinitionImpl implements DescribeDefinition{
	
//	private static final int DEAULT_HEIGHT = 100;
	
	private final List<DescribeDocument> paths = new ArrayList<>(2);
//	private final String value;
//	private final boolean href;
//	private final int height;
	
//	public DescribeDefinitionImpl(String value, String type, int height) {
//		this.value = value;
//		this.href = "href".equalsIgnoreCase(type);
//		this.height = height > 0 ? height : DEAULT_HEIGHT;
//	}
	
	private ParentContainer parent;
	private PlatformContext platformContext;
	
	private boolean showDocument = true;
	
	private String folder;
	
	public void init(PlatformContext platformContext, ParentContainer parent){
		this.platformContext = platformContext;
		this.parent = parent;
		
		for(DescribeDocument dDoc : paths){
			dDoc.init(platformContext.describeContext(), this);
		}
	}
	
	public boolean add(String name, String path){
		if(StringHelper.isEmpty(path)) return false;
		
		return paths.add(new DescribeDocument(name, path));
	}
	
	public PlatformContext getPlatformContext() {
		return platformContext;
	}
	
	public List<DescribeDocumentFile> resolve() throws Exception{
		List<DescribeDocumentFile> documentFiles = new ArrayList<>();
		DescribeContext describeContext = platformContext.describeContext();
		File documentFolder = describeContext.root();
		
		if(documentFolder.isDirectory()){
			
			File relativeFolder = documentFolder;
			if(StringHelper.isNotEmpty(folder)){
				relativeFolder = new File(documentFolder, folder);
			}
			
			for(DescribeDocument describeDocument : paths){
				if(describeDocument.isAbsolute()){
					describeDocument.resolve(documentFolder, documentFiles);
				}else if(relativeFolder.isDirectory()){
					describeDocument.resolve(relativeFolder, documentFiles);
				}
			}
		}
		return documentFiles;
	}
	
//	public String getValue() {
//		return value;
//	}
//	
//	public boolean isHref() {
//		return href;
//	}
//	
//	public int getHeight() {
//		return height;
//	}

	@Override
	public boolean isShowDocument() {
		return showDocument;
	}
	
	public void setShowDocument(boolean showDocument) {
		this.showDocument = showDocument;
	}

	public String getFolder() {
		return folder;
	}

	public void setFolder(String folder) {
		this.folder = folder;
	}
	
	public ParentContainer getParent() {
		return parent;
	}

	@Override
	public DescribeDocumentFile get(String key) {
		for(DescribeDocument dDoc : paths){
			DescribeDocumentFile ddFile = dDoc.get(key);
			if(null != ddFile) return ddFile;
		}
		return null;
	}
}
